package com.framework.file.controller;

import com.framework.common.domain.ResultData;
import com.framework.file.dto.FileManageUploadDto;
import com.framework.file.model.FileManage;
import com.framework.file.param.FileManageParam;
import com.framework.file.service.IFileManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * @description: 档案管理 Service
 * @author: lizurong
 * @version: 1.0
 * @date: 2021-04-01 16:53:14
 */
@RestController
@RequestMapping("/filemanage")
@Api(value = "FileManage", tags = "档案管理")
public class FileManageController {
    @Autowired
    private IFileManageService baseService;


    @ResponseBody
    @ApiOperation(value = "新增文件夹")
    @PostMapping(value = "/add")
    public ResultData add(@RequestBody FileManage param) {
        baseService.save(param);
        return ResultData.success();
    }

    /**
     * 批量上传文件(手动补录)
     *
     * @param files
     * @return
     */
    @ApiOperation("批量上传文件")
    @PostMapping(value = "/uploadBatch")
    public ResultData uploadBatch(@RequestParam List<MultipartFile> files, @RequestParam(required = false) String pid,
                                  @RequestParam(required = false) String savePeriod, @RequestParam(required = false) Integer isComplete,
                                  @RequestParam(required = false) String description, @RequestParam(required = false) String paperLocation,
                                  @RequestParam(required = false) String tag, @RequestParam String createrId, @RequestParam String createrComId
    ) throws Exception {
        for (MultipartFile file : files) {
            FileManageUploadDto dto = new FileManageUploadDto(file, pid, savePeriod, isComplete, description, 2, paperLocation, tag, createrId, createrComId);
            baseService.upload(dto);
        }
        return ResultData.success();
    }

    @ResponseBody
    @ApiOperation(value = "删除")
    @PostMapping(value = "/del")
    public ResultData delete(@RequestBody List<String> ids) {
        baseService.delete(ids);
        return ResultData.success();
    }

    @ResponseBody
    @ApiOperation(value = "编辑")
    @PostMapping(value = "/edit")
    public ResultData edit(@RequestBody FileManage param) {
        baseService.update(param);
        return ResultData.success();
    }

    @ApiOperation("获取分页")
    @PostMapping(value = "/listPage")
    public ResultData listPage(@RequestBody(required = false) FileManageParam param) {
        PageRequest pageRequest = PageRequest.of(param.getPageNo(), param.getPageSize());
        Page<FileManage> users = baseService.listPage(param, pageRequest);
        return ResultData.data(users);
    }

    @ApiOperation("获取左侧单位列表")
    @RequestMapping(value = "/getLeftList", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultData getLeftList(@RequestParam String loginComId) {
        return ResultData.data(baseService.getLeftList(loginComId));
    }

    @RequestMapping(value = "getTree", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(value = "获取树数据")
    public ResultData getTree(@RequestParam String id) {
        return ResultData.data(baseService.getTree(id));
    }

    @RequestMapping(value = "getTreeLocation", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(value = "获取当前树位置")
    public ResultData getTreeLocation(@RequestParam String id) {
        return ResultData.data(baseService.getTreeLocation(id));
    }

    @RequestMapping(value = "initSgCatalog", method = {RequestMethod.GET})
    @ApiOperation(value = "初始化施工目录")
    public ResultData initSgCatalog() throws Exception {
        baseService.initSgCatalog();
        return ResultData.success();
    }

    @RequestMapping(value = "initJlCatalog", method = {RequestMethod.GET})
    @ApiOperation(value = "初始化监理目录")
    public ResultData initJlCatalog() throws Exception {
        baseService.initJlCatalog();
        return ResultData.success();
    }

    @RequestMapping(value = "initYzCatalog", method = {RequestMethod.GET})
    @ApiOperation(value = "初始化业主目录")
    public ResultData initYzCatalog() throws Exception {
        baseService.initYzCatalog();
        return ResultData.success();
    }
}
